package com.yootk.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Map集合表达式
 */
public class Demo06_Map {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo03_Expression.class);

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
        test4();
    }

    /**
     * 访问Map集合数据
     */
    public static void test1() {
        Map<String, String> map = Map.of("yootk", "www.yootk.com", "jixianit", "www.jixianit.com", "edu", "edu.yootk.com");
        String content = "#{ #all['yootk'] }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("all", map);
        LOGGER.info("结果：{}", exp.getValue(context, String.class));
    }

    /**
     * 修改Map集合
     */
    public static void test2() {
        Map<String, String> map = new HashMap<>();
        map.put("yootk", "www.yootk.com");
        map.put("jixianit", "www.jixianit.com");
        map.put("edu", "edu.yootk.com");
        String content = "#{ #all['edu'] = 'book.yootk.com' }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("all", map);
        LOGGER.info("表达式计算结果：{}", exp.getValue(context, String.class));
        LOGGER.info("修改后Map集合：{}", map);
    }

    /**
     * 将Map集合转为List集合
     */
    public static void test3() {
        Map<String, String> map = Map.of("yootk", "www.yootk.com", "jixianit", "www.jixianit.com", "edu", "edu.yootk.com");
        String content = "#{ #all.![#this.key + ' - ' + #this.value] }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("all", map);
        LOGGER.info("结果：{}", exp.getValue(context, List.class));
    }

    /**
     * Map集合筛选
     */
    public static void test4() {
        Map<String, String> map = Map.of("yootk", "www.yootk.com", "jixianit", "www.jixianit.com", "edu", "edu.yootk.com");
        String content = "#{ #all.?[#this.key.contains('yootk')] }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("all", map);
        LOGGER.info("结果：{}", exp.getValue(context, Map.class));
    }
}
